<?php

namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\ForbiddenHttpException;
use yii\grid\GridView;
use yii\helpers\Html;
use backend\models\GrpClass;
use yii\base\Object;
use yii\base\Response;
use yii\widgets\ActiveForm;
use backend\models\UploadForm;
use backend\models\Image;
use yii\web\UploadedFile;
/**
 * 单品分类管理
 * @author suiyue
 *
 */
class SystemClassController extends BaseController
{
	/**
	 * (non-PHPdoc)行为
	 * @see \yii\base\Component::behaviors()
	 */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * (non-PHPdoc)前置函数
     * @see \yii\web\Controller::beforeAction()
     */
    public function beforeAction($action)
    {
    	$action = Yii::$app->controller->action->id;
    	$controller = Yii::$app->controller->id;
    	$action_list = array(
    	        'img-index',
    			'update',
    			'view',
    	);
    	 $access=$this->checkAccess($action_list,$action);//判断操作的action是否在actionlist里面
    	 $this->beforeOp('brand', $access);//身份识别 权限验证  action验证
    	 return true;
    }
	 
    /**
     *  
     */
    public function actionImgIndex(){
       /*  $searchModel = new GrpClass();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams); */
        $children = array();
        $child=array();
        $grandchild=array();
        $sql="select grp_class.*,sys_image.url imgurl from  grp_class left join sys_image on sys_image.id=grp_class.img_id ";
        $category =GrpClass::findBySql($sql)->asArray()->all();
        foreach ($category as $index => $row) {
            if (!empty($row['father'])) {
                $children[$row['father']][] = $row;
                $child[]=$row;
                unset($category[$index]);
            }
        }
        $sql="select grp_class.*,sys_image.url imgurl from grp_class left join sys_image on sys_image.id=grp_class.img_id where father!=0 " ;
        $g_list=GrpClass::findBySql($sql)->asArray()->all();
         foreach($child as $cindex=>$crow){
            foreach($g_list as $index=>$row){
                if($row['father']==$crow['id']){
                    $grandchild[$row['father']][]=$row;
               } 
            }
        }
        for($i=0;$i<count($child);$i++){
            if(!array_key_exists($child[$i]['id'], $grandchild)){
                $grandchild[$child[$i]['id']][]=array();
             }  
        }
        return $this->render('index', [
            'category' => $category,
            'children' => $children,
            'grandchild'=>$grandchild
        ]);
    }
   /**
    * 图片修改
    * @param   $id
    * @return  
    */
   public function actionUpdate($id){
       //$this->layout = 'main_small';
       $model =GrpClass::findOne($id);
       $oldImgid=intval($model->img_id);
       if ($model->load(Yii::$app->request->post())) {
           //图片上传
           $Uploadmodel = new UploadForm();
           $Uploadmodel->scenario = 'no_need_file_name';
           $Uploadmodel->image_upload = UploadedFile::getInstance($model,'img_id');
           if ($Uploadmodel->image_upload && $Uploadmodel->validate()) {
               if($oldImgid==0){
                   $arr = Image::save_image_to_files($Uploadmodel,196,'单品分类图片','item_default_image_thumb','item_default_image_small');
                   $model->img_id = $arr['img_url_id'];
                   $model->save();
               }else{
                   $arr = Image::save_image_to_files($Uploadmodel,196,'单品分类图片','item_default_image_thumb','item_default_image_small',$model->img_id,$model->img_id,$model->img_id);
               }
               return $this->redirect(['view', 'id' => $model->id]);
           }else{
               return $this->redirect(['update', 'id' => $model->id]);
           }
           
       } else{
           $model=GrpClass::findOne($id);
           return $this->render('update', [
               'model' => $model
           ]);
       }
   }
  
    /**
     * 详情页
     * @param   $id
     */
    public function actionView($id){
        //$this->layout = 'main_small';
        $sql="select grp_class.*,sys_image.url imgurl 
             from grp_class left join sys_image  on sys_image.id=grp_class.img_id
            where grp_class.id=:id";
        $model=GrpClass::findBySql($sql,array(":id"=>$id))->one();
        return $this->render('view', [
            'model' => $model
        ]);
    }
    /**
     * 查找单个
     * @param   $id
     * @throws NotFoundHttpException
     */
    protected function findModel($id)
    {
        if (($model = GrpClass::findOne($id)) !== null) {
            $model = GrpClass::find()->where('id='.$id)->one();
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
   
    
}
